package com.leyou.controller;

import com.leyou.dto.SearchParamDTO;
import com.leyou.entity.Goods;
import com.leyou.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author jshstart
 * @creat 2020-12-15 22:14
 */
@RestController
@RequestMapping("/goods")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @GetMapping("/init")
    public ResponseEntity<String> init(){
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入数据成功");
    }
    @GetMapping("/suggestion")
    public Mono<List<String>> queryBySuggest(@RequestParam("key") String key){
        System.out.println("key = " + key);
      return   searchService.queryBySuggest(key);
    }
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchByPage(@RequestBody SearchParamDTO searchParamDTO){
      return   searchService.searchByPage(searchParamDTO);
    }
    @PostMapping("/filter")
    public  Map<String,List<?>>  searchByAgg(@RequestBody SearchParamDTO searchParamDTO) throws IOException {
        return   searchService.searchByAgg(searchParamDTO);
    }
}
